package com.example.avmanagerweb.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.avmanagerweb.entity.Audiogroup;
import com.example.avmanagerweb.entity.Audiogroupinpt;
import com.example.avmanagerweb.entity.Audiogroupstaff;
import com.example.avmanagerweb.entity.SysUserinfo;
import com.example.avmanagerweb.entity.vo.AudiogroupVo;
import com.example.avmanagerweb.redisService.RedisService;
import com.example.avmanagerweb.result.RLIST;
import com.example.avmanagerweb.service.AudiogroupService;
import com.example.avmanagerweb.service.AudiogroupinptService;
import com.example.avmanagerweb.service.AudiogroupstaffService;
import com.example.avmanagerweb.service.SysUserinfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2023-08-16
 */
@RestController
@RequestMapping("/audiogroup")
public class AudiogroupController {
    @Autowired
    private AudiogroupService audiogroupService;

    @Autowired
    private AudiogroupinptService audiogroupinptService;

    @Autowired
    private AudiogroupstaffService audiogroupstaffService;

    @Autowired
    private SysUserinfoService sysUserinfoService;

    @Autowired
    private RedisService redisService;

//    2.	对讲群组增删改查接口

    @GetMapping(value = "/getGroup")
    public RLIST getGroup(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        System.out.println(value);
        if( value == null || value == 0 ) {
            return RLIST.error().message("token不存在");
        }
        String  json = (String) redisService.getValue(value+"");
        JSONObject jsonObject = JSONObject.parseObject(json);
        SysUserinfo sysUserinfo = JSON.toJavaObject(jsonObject, SysUserinfo.class);


        if(sysUserinfo == null){
            return RLIST.error().message("无法匹配管理员ID异常");
        }

        if ( (sysUserinfo.getUissysmana() == null || !sysUserinfo.getUissysmana()) && (sysUserinfo.getUisdepartmana() == null || !sysUserinfo.getUisdepartmana())) {
            return RLIST.error().message("账号权限不够");
        }


        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.eq("AdminID", sysUserinfo.getUid());
        List<Audiogroupinpt> list = audiogroupinptService.list(wrapper);
        return RLIST.ok().data("data",list);
    }

    @PostMapping(value = "/addGroup")
    public RLIST addGroup(@RequestBody AudiogroupVo audiogroupVo, HttpServletRequest request){
        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        System.out.println(value);
        if( value == null || value == 0 ) {
            return RLIST.error().message("token异常 出现bug");
        }
        String  json = (String) redisService.getValue(value+"");
        JSONObject jsonObject = JSONObject.parseObject(json);
        SysUserinfo sysUserinfo = JSON.toJavaObject(jsonObject, SysUserinfo.class);

        if(sysUserinfo == null){
            return RLIST.error().message("无法匹配管理员ID异常");
        }

        if ( (sysUserinfo.getUissysmana() == null || !sysUserinfo.getUissysmana()) && (sysUserinfo.getUisdepartmana() == null || !sysUserinfo.getUisdepartmana())) {
            return RLIST.error().message("账号权限不够");
        }


        if(StringUtils.isEmpty(audiogroupVo.getAudiogroupname())){
            return RLIST.error().message("名称为空");
        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("AudioGroupName", audiogroupVo.getAudiogroupname());
        int count = audiogroupinptService.count(wrapper);
        if(count>0){
            return RLIST.error().message("群名重复");
        }






        Audiogroup audiogroup = new Audiogroup();
        audiogroup.setAudiogroupname(audiogroupVo.getAudiogroupname());
        audiogroup.setGrouptype(audiogroupVo.getGrouptype());
        audiogroup.setAdminId(sysUserinfo.getUid());

        boolean save = audiogroupService.save(audiogroup);
        if(!save){
            return RLIST.error().message("组添加失败");
        }
        if(audiogroupVo.getUserArr() !=null && audiogroupVo.getUserArr().length != 0){
            List<Audiogroupstaff> audiogroupstaffs = new ArrayList<>();
            Integer[] userArr = audiogroupVo.getUserArr();
            for (Integer integer : userArr) {
                Audiogroupstaff audiogroupstaff = new Audiogroupstaff();
                audiogroupstaff.setAudiogroupid(audiogroup.getAudiogroupid());
                audiogroupstaff.setUserid(integer);

                audiogroupstaffs.add(audiogroupstaff);
            }
            boolean b = audiogroupstaffService.saveBatch2(audiogroupstaffs);
            if(!b){
                return RLIST.error().message("成员添加失败");
            }
        }
        return RLIST.ok();
    }

    @DeleteMapping(value = "/delGroup")
    public RLIST delGroup( @RequestParam("id") int id, HttpServletRequest request){
        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        System.out.println(value);
        if( value == null || value == 0 ) {
            return RLIST.error().message("token异常 出现bug");
        }
        String  json = (String) redisService.getValue(value+"");
        JSONObject jsonObject = JSONObject.parseObject(json);
        SysUserinfo sysUserinfo = JSON.toJavaObject(jsonObject, SysUserinfo.class);



        if(id <= 0){
            return RLIST.error().message("ID异常");
        }


        if(sysUserinfo == null){
            return RLIST.error().message("无法匹配管理员ID异常");
        }

        if ( (sysUserinfo.getUissysmana() == null || !sysUserinfo.getUissysmana()) && (sysUserinfo.getUisdepartmana() == null || !sysUserinfo.getUisdepartmana())) {
            return RLIST.error().message("账号权限不够");
        }

        QueryWrapper wrapper2 = new QueryWrapper();
        wrapper2.eq("AudioGroupID",id);
        wrapper2.eq("AdminID",sysUserinfo.getUid());
        boolean b = audiogroupService.remove(wrapper2);
        if(b){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("AudioGroupID",id);
            audiogroupstaffService.remove(wrapper);
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }

    @PostMapping(value = "/alterGroup")
    public RLIST alterGroup(@RequestBody Audiogroup audiogroup, HttpServletRequest request){
        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        System.out.println(value);
        if( value == null || value == 0 ) {
            return RLIST.error().message("token异常 出现bug");
        }
        String  json = (String) redisService.getValue(value+"");
        JSONObject jsonObject = JSONObject.parseObject(json);
        SysUserinfo sysUserinfo = JSON.toJavaObject(jsonObject, SysUserinfo.class);




        if(audiogroup.getAdminId() <= 0){
            return RLIST.error().message("adminId异常");
        }


        if(sysUserinfo == null){
            return RLIST.error().message("无法匹配管理员ID异常");
        }

        if ( (sysUserinfo.getUissysmana() == null || !sysUserinfo.getUissysmana()) && (sysUserinfo.getUisdepartmana() == null || !sysUserinfo.getUisdepartmana())) {
            return RLIST.error().message("账号权限不够");
        }

        if(audiogroup.getAudiogroupid() ==null && audiogroup.getAudiogroupid() ==0){
            return RLIST.error().message("组id异常");
        }
        if( StringUtils.isEmpty(audiogroup.getAudiogroupname()) ){
            return RLIST.error().message("修改名异常异常");
        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("AudioGroupID",audiogroup.getAudiogroupid());
        wrapper.eq("AdminID",sysUserinfo.getUid());
        audiogroup.setAdminId(null);
        boolean b = audiogroupService.update(audiogroup,wrapper);
        if(b){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }




}

